package com.wythe.mall.adapter;

import android.content.Context;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.wythe.mall.R;
import com.wythe.mall.model.CarBean;

import java.util.ArrayList;

public class GoodsCartListAdapter extends RecyclerView.Adapter<GoodsCartListAdapter.ViewHolder> {

    private ArrayList<CarBean> arrayList;
    private OnItemClickListener onItemClickListener;
    private Context mContext;

    public GoodsCartListAdapter(Context context, ArrayList<CarBean> arrayList) {
        mContext = context;
        this.arrayList = arrayList;
        this.onItemClickListener = null;
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final CarBean bean = arrayList.get(position);
        holder.mainCheckBox.setSelected(bean.isClick());
        holder.mainCheckBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (onItemClickListener != null) {
                    onItemClickListener.onClick(position, holder.mainCheckBox);
                }
            }
        });
        Glide.with(mContext)
                .load(bean.getIcon())
                .dontAnimate()
                .crossFade()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(holder.mainImageView);
        holder.nameTextView.setText(bean.getTitle());
        holder.mobileTextView.setText(bean.getSpecs());
        holder.moneyTextView.setText("￥" + bean.getPrice());
        holder.numberEditText.setText(bean.getCount() + "");
        holder.activityTextView.setText(bean.getShop());
        holder.addTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null) {
                    onItemClickListener.onAddClick(position, holder.numberEditText);
                }
            }
        });

        holder.subTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null) {
                    onItemClickListener.onReduceClick(position, holder.numberEditText);
                }
            }
        });
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup group, int viewType) {
        View view = LayoutInflater.from(group.getContext()).inflate(R.layout.item_list_goods_cart, group, false);
        return new ViewHolder(view);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {

        this.onItemClickListener = listener;

    }

    public void setDatas(ArrayList<CarBean> list) {
        arrayList = list;
        notifyDataSetChanged();
    }

    public interface OnItemClickListener {
        void onClick(int position, ImageView mainCheckBox);

        void onAddClick(int position, AppCompatEditText view);

        void onReduceClick(int position, AppCompatEditText view);
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private RelativeLayout mainRelativeLayout;
        private ImageView mainCheckBox;
        private AppCompatImageView mainImageView;
        private AppCompatTextView nameTextView;
        private AppCompatTextView moneyTextView;
        private AppCompatTextView mobileTextView;
        private AppCompatTextView activityTextView;
        private AppCompatImageView deleteImageView;
        private ImageView addTextView;
        private AppCompatEditText numberEditText;
        private ImageView subTextView;

        private ViewHolder(View view) {
            super(view);
            mainRelativeLayout = (RelativeLayout) view.findViewById(R.id.mainRelativeLayout);
            mainCheckBox = (ImageView) view.findViewById(R.id.mainCheckBox);
            mainImageView = (AppCompatImageView) view.findViewById(R.id.mainImageView);
            nameTextView = (AppCompatTextView) view.findViewById(R.id.nameTextView);
            moneyTextView = (AppCompatTextView) view.findViewById(R.id.moneyTextView);
            mobileTextView = (AppCompatTextView) view.findViewById(R.id.mobileTextView);
            activityTextView = (AppCompatTextView) view.findViewById(R.id.activityTextView);
            addTextView = (ImageView) view.findViewById(R.id.addTextView);
            numberEditText = (AppCompatEditText) view.findViewById(R.id.numberEditText);
            subTextView = (ImageView) view.findViewById(R.id.subTextView);
        }

    }

}
